package com.songlanyun.modules.refillcard.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.modules.ad.entity.AdEntity;
import com.songlanyun.modules.exception.AdException;
import com.songlanyun.modules.exception.GoodsExceptionEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.refillcard.dao.SaleRefillCardDao;
import com.songlanyun.modules.refillcard.entity.SaleRefillCardEntity;
import com.songlanyun.modules.refillcard.service.SaleRefillCardService;


@Service("yxtSaleRefillCardService")
public class SaleRefillCardServiceImpl extends ServiceImpl<SaleRefillCardDao, SaleRefillCardEntity> implements SaleRefillCardService {

    /**
     * 列表查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String name = MapUtil.getStr(params, "name");//卡名称
        Integer added = MapUtil.getInt(params, "added");//是否上架

        IPage<SaleRefillCardEntity> page = this.page(
                new Query<SaleRefillCardEntity>().getPage(params),
                new LambdaQueryWrapper<SaleRefillCardEntity>()
                        .eq(added != null, SaleRefillCardEntity::getAdded, added)
                        .like(StringUtils.isNotBlank(name), SaleRefillCardEntity::getName, name)
                        .orderByDesc(SaleRefillCardEntity::getCreateTime)
        );

        return new PageUtils(page);
    }

    /**
     * @param id
     * @param isThrow 是否抛错
     * @return
     */
    @Override
    public SaleRefillCardEntity getById(Long id, boolean isThrow) {
        SaleRefillCardEntity entity = this.getById(id);
        if (entity == null) {
            if (isThrow) {
                throw new RRException(GoodsExceptionEnum.CARD_NOT_EXIST);
            }
        }
        return entity;

    }

    @Override
    public SaleRefillCardEntity getByIdAndUp(Long refillCardId) {

        SaleRefillCardEntity entity = getById(refillCardId);
        if (Constant.UP != entity.getAdded()) {
            throw new RRException(GoodsExceptionEnum.CARD_NOT_UP);
        }
        return entity;
    }

}
